#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "los_queue.h"
#include "los_task.h"
#include "cmsis_os2.h"


static UINT32 g_queue;
#define BUFFER_LEN 50

void send_Entry(void *argument)
{
    uint32_t i = 0;
    char abuf[] = "test is message x";
    uint16_t len = sizeof(abuf);

    while (i < 5) {
        abuf[len -2] = '0' + i;
        i++;

        osStatus_t status = osMessageQueuePut(g_queue, abuf, 0, 0);
        if(status != osOK) {
            printf("send message failure,error:%d\n", status);
        }

        osDelay(5);  
    }
}

void recv_Entry(void *argument)
{
    char readBuf[BUFFER_LEN] = {0};
    osStatus_t status;

    while (1) {
        status = osMessageQueueGet(g_queue, readBuf, NULL, 0);
        if(status != osOK) {
            printf("recv message failure,error:%d\n", status);
            break;
        }

        printf("recv message:%s\n", readBuf);
        osDelay(5);
    }

    osMessageQueueDelete(g_queue);
    printf("delete the queue success!\n");
}

void Example_CreateTask(void)
{
    osThreadAttr_t attr = {
        .name = "sendQueue",
        .stack_size = 1024,
        .priority = osPriorityNormal,
    };

    osKernelLock();  
    
    
    g_queue = osMessageQueueNew(5, sizeof("test is message x"), NULL);
    printf("create the queue success!\n");

    
    osThreadNew(send_Entry, NULL, &attr);
    
    
    attr.name = "recvQueue";
    osThreadNew(recv_Entry, NULL, &attr);

    osKernelUnlock();  
}


APP_FEATURE_INIT(Example_CreateTask);